package com.wlr.study.leetcode;

/**
 * @author HustleLr
 * @version 1.0.0
 * @date 2023/10/3 15:33
 * @describe LeetCode238
 */
public class LeetCode238 {
    public static int[] productExceptSelf(int[] nums) {
        // nums = [1,2,3,4];
        // [24, 12, 8, 4];
//        int[] preResult = new int[nums.length];
//        int[] sufResult = new int[nums.length];
        int[] result = new int[nums.length];
//        preResult[0] = 1;
//        sufResult[nums.length - 1] = 1;
//        for (int i = 1; i < preResult.length; i++) {
//            preResult[i] = nums[i - 1] * preResult[i - 1];
//        }
//        for (int i = nums.length - 2; i >= 0; i--) {
//            sufResult[i] = nums[i + 1] * sufResult[i + 1];
//        }
//        for (int i = 0; i < nums.length; i++) {
//            result[i]=preResult[i]*sufResult[i];
//        }
        int length = nums.length;
        int left = 1, right = 1;
        for (int i = 0; i < length; i++) {
            result[i] = 1;
        }
        for (int i = 0; i < length; i++) {
            result[i] *= left;
            result[length - i - 1] *= right;
            left *= nums[i];
            right *= nums[length - i - 1];
        }
        return result;
    }

    public static void main(String[] args) {
        for (int i : LeetCode238.productExceptSelf(new int[]{1, 2, 3, 4})) {
            System.out.println(i);
        }
    }
}
